<?php
class Manageaccount_Data_Updateform {
	
	private $form;
	private $database;
	public $data;
	private $secure;
	
	public function __construct(){
		$this->form = new Core_Driver_Firstform();
		$this->database = new Core_Driver_MysqlImproved();
		$this->secure = new Core_Data_Secure();
	}
	
	public function getData($username){
		$this->data = $this->getValues($username);
		$this->data = $this->secure->parseData($this->data);
		$article = $this->getForm($this->data);
		return $article;
	}
	
	private function getForm(array $userData){
		
		$userDataForm .= $this->form->startForm(
				array(
						"id" => "updateData",
						"name" => "updateData",
						"method" => "post",
						"action" => SERVER_ROOT . "?manageaccount",
						"width" => "50%"
				)
		);
		$userDataForm .= $this->form->inputForm(
			array(
			"type" => "hidden",
					"name" => "updateAccount",
					"value" => $userData['users_username']
		),
				null,
				null
		);
		$userDataForm .= MANAGE_ACCOUNT_MESSAGE;
		$userDataForm .= "<ul>";
		$userDataForm .= "<li>";
		$userDataForm .= $this->form->labelForm(
				array(
						"for" => "userName"
				),
				MANAGE_ACCOUNT_USERNAME,
				null
		);
		$userDataForm .= $this->form->inputForm(
				array(
						"type" => "text",
						"name" => "username",
						"id" => "username",
						"value" => $userData['users_username'],
						"maxlength" => "20"
				),
				"required autofocus",
				null
		);
		$userDataForm .= "</li>";
		$userDataForm .= "<li>";
		$userDataForm .= $this->form->labelForm(
				array(
						"for" => "realname"
				),
				MANAGE_ACCOUNT_REALNAME,
				null
		);
		$userDataForm .= $this->form->inputForm(
				array(
						"type" => "text",
						"name" => "realname",
						"id" => "realname",
						"value" => $userData['users_realname'],
						"maxlength" => "20"
				),
				"required autofocus",
				null
		);
		$userDataForm .= "</li>";
		$userDataForm .= "<li>";
		$userDataForm .= $this->form->labelForm(
				array(
						"for" => "language"
				),
				MANAGE_ACCOUNT_LANGUAGE,
				null
		);
		$userDataForm .= $this->form->selectForm(
				array(
						"id" => "language",
						"name" => "language"
				),
				null,
				array(
			"pl" => "Polski",
						"en" => "English"
		)
		);
		$userDataForm .= "</li>";
		$userDataForm .= "<li>";
		$userDataForm .= $this->form->labelForm(
				array(
						"for" => "email"
				),
				MANAGE_ACCOUNT_EMAIL,
				null
		);
		$userDataForm .= $this->form->inputForm(
				array(
						"type" => "email",
						"name" => "email",
						"id" => "email",
						"value" => $userData['users_email'],
						"maxlegth" => "20"
				),
				"required autofocus",
				null
		);
		
		$userDataForm .= "</li>";
		$userDataForm .= "<li>";
		$userDataForm .= $this->form->labelForm(
				array(
						"for" => "password"
				),
				MANAGE_ACCOUNT_PASSWORD,
				null
		);
		$userDataForm .= $this->form->inputForm(
				array(
						"type" => "password",
						"name" => "password",
						"id" => "password",
						"value" => $userData['users_password'],
						"maxlegth" => "20"
				),
				"required",
				null
		);
		$userDataForm .= "</li>";
		$userDataForm .= "</ul>";
		$userDataForm .= '<div class="buttons">';
		$userDataForm .= $this->form->inputForm(
				array(
						"type" => "submit",
						"name" => "submit",
						"value" => MANAGE_ACCOUNT_BUTTON,
						"id" => "submit"
				),
				null,
				null
		);
		$userDataForm .= "</div>";
		$userDataForm .= $this->form->endForm();
		return $userDataForm;
	}
	private function getValues($username){
		$this->database->connect();
		$username = $this->database->escape($username);
		$this->database->prepare(
			"
				SELECT * FROM users WHERE users_username = '$username';
				"
		);
		$this->database->query();
		$userData = $this->database->fetch('array');
		return $userData;
	}
	
}